/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.Service
*        文件名:             SequenceService.svc.cs
*        创建系统时间:       2012/11/2 19:07:06
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.Service
{
    using System;
    using System.Data;
    using System.Reflection;
    using System.ServiceModel;
    using ECP.DataAccess;
    using ECP.DataAccess.Utilities;
    using ECP.Interface;
    using ECP.Model;
    using ECP.Utilities;

    [ServiceBehavior(Namespace = "http://jiangguang.net.cn/")]
    public partial class SequenceService : MarshalByRefObject, ISequenceService
    {
        private static SequenceService instance = null;
        private static object locker = new object();

        public string Add(BaseUserInfo userInfo, BaseSequenceEntity sequenceEntity, out string statusCode, out string statusMessage)
        {
            string str = string.Empty;
            statusCode = string.Empty;
            statusMessage = string.Empty;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseSequenceDao dao = new BaseSequenceDao(dbHelper, userInfo);
                str = dao.Add(sequenceEntity, out statusCode);
                statusMessage = dao.GetStateMessage(statusCode);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return str;
        }

        public string Add(BaseUserInfo userInfo, DataTable dataTable, out string statusCode, out string statusMessage)
        {
            BaseSequenceEntity sequenceEntity = new BaseSequenceEntity(dataTable);
            return this.Add(userInfo, sequenceEntity, out statusCode, out statusMessage);
        }

        public DataTable BatchDelete(BaseUserInfo userInfo, string[] ids)
        {
            DataTable dT = new DataTable(BaseSequenceTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseSequenceDao.Instance.BatchDelete(dbHelper, ids);
                dT = BaseSequenceDao.Instance.GetDT(dbHelper);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return dT;
        }

        public DataTable Delete(BaseUserInfo userInfo, string id)
        {
            DataTable dT = new DataTable(BaseSequenceTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseSequenceDao.Instance.Delete(dbHelper, id);
                dT = BaseSequenceDao.Instance.GetDT(dbHelper);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return dT;
        }

        public DataTable Get(BaseUserInfo userInfo, string id)
        {
            DataTable table = new DataTable(BaseBusinessCardTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                table = new BaseSequenceDao(dbHelper, userInfo).Get(id);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return table;
        }

        public string[] GetBatchSequence(BaseUserInfo userInfo, string fullName, int count)
        {
            string[] strArray = new string[0];
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                strArray = this.GetBatchSequence(dbHelper, userInfo, fullName, count);
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return strArray;
        }

        public string[] GetBatchSequence(IDbHelper dbHelper, BaseUserInfo userInfo, string fullName, int count)
        {
            return BaseSequenceDao.Instance.GetBatchSequence(dbHelper, fullName, count);
        }

        public string GetDegression(BaseUserInfo userInfo, string fullName)
        {
            string str = string.Empty;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                str = this.GetDegression(dbHelper, userInfo, fullName);
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return str;
        }

        public string GetDegression(IDbHelper dbHelper, BaseUserInfo userInfo, string fullName)
        {
            return BaseSequenceDao.Instance.GetDegression(dbHelper, fullName);
        }

        public DataTable GetList(BaseUserInfo userInfo)
        {
            DataTable dT = new DataTable(BaseSequenceTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                dT = BaseSequenceDao.Instance.GetDT(dbHelper);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return dT;
        }

        public string GetSequence(BaseUserInfo userInfo, string fullName)
        {
            string str = string.Empty;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                str = this.GetSequence(dbHelper, userInfo, fullName);
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return str;
        }

        public string GetSequence(IDbHelper dbHelper, BaseUserInfo userInfo, string fullName)
        {
            return BaseSequenceDao.Instance.GetSequence(dbHelper, fullName);
        }

        public void Load()
        {
        }

        public DataTable Reset(BaseUserInfo userInfo, string[] ids)
        {
            DataTable dT = new DataTable(BaseSequenceTable.TableName);
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseSequenceDao.Instance.Reset(dbHelper, ids);
                dT = BaseSequenceDao.Instance.GetDT(dbHelper);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return dT;
        }

        public int Update(BaseUserInfo userInfo, BaseSequenceEntity baseSequenceEntity, out string statusCode, out string statusMessage)
        {
            statusCode = string.Empty;
            statusMessage = string.Empty;
            int num = 0;
            IDbHelper dbHelper = DbHelperFactory.GetHelper();
            try
            {
                dbHelper.Open();
                BaseSequenceDao dao = new BaseSequenceDao(dbHelper, userInfo);
                num = dao.Update(baseSequenceEntity, out statusCode);
                statusMessage = dao.GetStateMessage(statusCode);
                BaseLogDao.Instance.Add(dbHelper, userInfo, MethodBase.GetCurrentMethod());
            }
            catch (Exception exception)
            {
                BaseExceptionDao.LogException(dbHelper, userInfo, exception);
                throw exception;
            }
            finally
            {
                dbHelper.Close();
            }
            return num;
        }

        public int Update(BaseUserInfo userInfo, DataTable dataTable, out string statusCode, out string statusMessage)
        {
            BaseSequenceEntity baseSequenceEntity = new BaseSequenceEntity(dataTable);
            return this.Update(userInfo, baseSequenceEntity, out statusCode, out statusMessage);
        }

        public static SequenceService Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new SequenceService();
                        }
                    }
                }
                return instance;
            }
        }

        public BaseSequenceEntity GetEntity(BaseUserInfo userInfo, string id)
        {
            return new BaseSequenceEntity(this.Get(userInfo, id));
        }
    }
}